package ict;

import java.util.ArrayList;

import ict.mobile.R;
import android.app.Activity;
import android.os.Bundle;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.*;
import android.view.*;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class modifyorderActivity extends Activity implements
		AdapterView.OnItemSelectedListener {

	SQLiteDatabase db;
	String sql;
	Cursor cursor = null;
	String[] columns = { "mid", "name", "password", "age" };
	String dataStrHeader = String.format("%4s %-12s %-9s %3s\n", "Mid", "Name",
			"Password", "Age");
	String dataStr;
	
	TextView modifyOrderLoginNameTV;
	EditText modifyQET1, modifyQET2, modifyQET3, modifyQET4, modifyQET5;
	
	String result;
	String pos;
	ArrayList IDList = new ArrayList();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.modifyorder);

		findViewBy();
		addOrderIDToList();

		Spinner spin = (Spinner) findViewById(R.id.orderCodespinner);
		spin.setOnItemSelectedListener(this);
		ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, IDList);
		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(aa);

	}

	private void findViewBy() {
		// TODO Auto-generated method stub
		
		modifyOrderLoginNameTV = (TextView) findViewById(R.id.modifyOrderLoginNameTV);
		modifyQET1 = (EditText) findViewById(R.id.modifyQET1);
		modifyQET2 = (EditText) findViewById(R.id.modifyQET2);
		modifyQET3 = (EditText) findViewById(R.id.modifyQET3);
		modifyQET4 = (EditText) findViewById(R.id.modifyQET4);
		modifyQET5 = (EditText) findViewById(R.id.modifyQET5);

	}
	

	// //Update
	public void updateQuantity(String x) {
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READWRITE);

			checkFiller(modifyQET1.getText().toString());
			modifyQET1.setText(getCheckFiller());
			
			checkFiller(modifyQET2.getText().toString());
			modifyQET2.setText(getCheckFiller());
			
			checkFiller(modifyQET3.getText().toString());
			modifyQET3.setText(getCheckFiller());
			
			checkFiller(modifyQET4.getText().toString());
			modifyQET4.setText(getCheckFiller());
			
			checkFiller(modifyQET5.getText().toString());
			modifyQET5.setText(getCheckFiller());
			
			
			String sql = "Update productOrder set prod01 = " + modifyQET1.getText().toString() + ","
					+"prod02 = " + modifyQET2.getText().toString()  +","
					+"prod03 = " + modifyQET3.getText().toString() + ","
					+"prod04 = " + modifyQET4.getText().toString() + ","
					+"prod05 = " + modifyQET5.getText().toString() 
					+ " where OrderID = '" + x +"';";
			/*
			String sql = "Update Product set Quantity = "
					+ updateQuantityET.getText().toString()
					+ " where ProductCode = '" + x +"';";
					*/
			db.execSQL(sql);
			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}
	
	public void onModifyOrder(View view) {
		updateQuantity(pos);
		finish();
		
	}
	
	public void checkFiller(String target){
		if(target==null||target.equals("")){
			target = "0";
		}
		
		result = target;
	}
	
	public String getCheckFiller(){
		
		return result;
	}
	/////display productOrder
	public void selectAllFromproductOrder(String productC){
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);

			cursor = db.rawQuery("select * from productOrder Where OrderID = '" + productC + "'", null);
			//cursor = db.rawQuery("select * from productOrder Where OrderID = " + productC, null);
			
			

			//"CREATE TABLE productOrder (OrderID text PRIMARY KEY, ShopCode text, StaffID int, LoginName text, date text, prod01 int, prod02 int , prod03 int , prod04 int,  prod05 int);";
			String dataStr = "";
			while (cursor.moveToNext()) {
				// or cursor.getInt(0);
				String OrderID = cursor.getString(0);
				String ShopCode = cursor.getString(1);
				int StaffID = cursor.getInt(2);
				String LoginName = cursor.getString(3);
				String date = cursor.getString(4);
				String prod01 = cursor.getString(5);
				String prod02 = cursor.getString(6);
				String prod03 = cursor.getString(7);
				String prod04 = cursor.getString(8);
				String prod05 = cursor.getString(9);
				
				modifyOrderLoginNameTV.setText(LoginName);
				modifyQET1.setText(prod01);
				modifyQET2.setText(prod02);
				modifyQET3.setText(prod03);
				modifyQET4.setText(prod04);
				modifyQET5.setText(prod05);
				
				
			}
			
			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}
	
	//add  OrderID to spinner
	private void addOrderIDToList() {
		// TODO Auto-generated method stub
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);

			cursor = db.rawQuery("select * from productOrder ;", null);
			// Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();

			while (cursor.moveToNext()) {
				// or cursor.getInt(0);
				String OrderID = cursor.getString(0);
				IDList.add(OrderID);
			}

			// Toast.makeText(this, IDList.toString(),
			// Toast.LENGTH_LONG).show();

			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}




	public void onItemSelected(AdapterView parent, View v, int position, long id) {
		// TODO Auto-generated method stub
		selectAllFromproductOrder(""+IDList.get(position));
		 pos = ""+IDList.get(position);
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

}
